import React, { Component } from 'react';
import './index.css';
import {nanoid} from 'nanoid';//必须加括号,否则出不来

export default class Header extends Component {
    handleKeyUp = (e) =>{
        const { keyCode,target } = e;
        if(keyCode !== 13 ) return;//不回车不处理
        if(target.value.trim() === ''){
            alert('输入不能为空');
            return;
        }
        const todoObj = {id:nanoid(),name:e.target.value,done:false};
        this.props.addTodo(todoObj);
    }
    
    render() {
        return (
            <div className="todo-header">
                <input type="text" onKeyUp={this.handleKeyUp} placeholder="请输入你的任务名称，按回车键确认" />
            </div>
        )
    }
}
